package m.jclecai.com.adapter.KSPagerAdapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import m.jclecai.com.R;
import m.jclecai.com.inter.IAdapterItemSelect;

/**
 * Created by hp on 2017/5/4.
 * 二不同
 */

public class BetTwoDifferentAdapter extends BaseAdapter implements View.OnClickListener {

    private final String TAG = "二不同号";

    private final Context context;
    private final IAdapterItemSelect i;
    private String[] str;
    private boolean isClear;
    private List<String> betParamete = new LinkedList<>(); //用户投注参数 快速选择
    Map<Integer, Boolean> selectMap = new HashMap<>(); //自选
    //随机数
    private int randomNumber;
    private ListView mListView;
    private boolean mShowMissData;
    private String[] mMissData = new String[]{};

    //获取投注参数
    public List<String> getBetParamete() {
        for (Map.Entry<Integer, Boolean> entry : selectMap.entrySet()) {
            if (entry.getValue()) {
                betParamete.add(entry.getKey() + "");
            }
        }
        return betParamete;
    }

    public void clearSelect() {
        isClear = true;
    }

    public void setRandomNumber(int randomNumber) {
      /*  this.randomNumber = randomNumber;
        isClear = true;*/

        clearAll();
        List<String> stringList = Arrays.asList(str);
        int numPosition = stringList.indexOf(randomNumber + "");
        int holderPostion = numPosition / 5;
        int itemPostion = numPosition % 5;

        View view = mListView.getChildAt(holderPostion);
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        switch (itemPostion) {
            case 0:
                setSelected(viewHolder, viewHolder.tv_number_one, viewHolder.rl_one);
                break;
            case 1:
                setSelected(viewHolder, viewHolder.tv_number_two, viewHolder.rl_two);
                break;
            case 2:
                setSelected(viewHolder, viewHolder.tv_number_three, viewHolder.rl_three);
                break;
            case 3:
                setSelected(viewHolder, viewHolder.tv_number_four, viewHolder.rl_four);
                break;
            case 4:
                setSelected(viewHolder, viewHolder.tv_number_five, viewHolder.rl_five);
                break;
        }
    }

    public BetTwoDifferentAdapter(Context context, IAdapterItemSelect i) {
        this.context = context;
        this.i = i;
    }

    public void setData(String[] str) {
        this.str = str;
    }

    @Override
    public int getCount() {
        return str.length / 5;
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            holder.position = position;
            convertView = View.inflate(context, R.layout.ks_item_base, null);

            holder.rl_one = (RelativeLayout) convertView.findViewById(R.id.rl_one);
            holder.rl_one.setOnClickListener(this);
            holder.rl_two = (RelativeLayout) convertView.findViewById(R.id.rl_two);
            holder.rl_two.setOnClickListener(this);
            holder.rl_three = (RelativeLayout) convertView.findViewById(R.id.rl_three);
            holder.rl_three.setOnClickListener(this);
            holder.rl_four = (RelativeLayout) convertView.findViewById(R.id.rl_four);
            holder.rl_four.setOnClickListener(this);
            holder.rl_five = (RelativeLayout) convertView.findViewById(R.id.rl_five);
            holder.rl_five.setOnClickListener(this);


            holder.tv_miss_data_one = ((TextView) convertView.findViewById(R.id.tv_miss_data_one));
            holder.tv_miss_data_two = ((TextView) convertView.findViewById(R.id.tv_miss_data_two));
            holder.tv_miss_data_three = ((TextView) convertView.findViewById(R.id.tv_miss_data_three));
            holder.tv_miss_data_four = ((TextView) convertView.findViewById(R.id.tv_miss_data_four));
            holder.tv_miss_data_five = ((TextView) convertView.findViewById(R.id.tv_miss_data_five));

            holder.rl_one.setTag(holder);
            holder.rl_two.setTag(holder);
            holder.rl_three.setTag(holder);
            holder.rl_four.setTag(holder);
            holder.rl_five.setTag(holder);

            holder.tv_number_one = (TextView) convertView.findViewById(R.id.tv_number_one);
            holder.tv_number_two = (TextView) convertView.findViewById(R.id.tv_number_two);
            holder.tv_number_three = (TextView) convertView.findViewById(R.id.tv_number_three);
            holder.tv_number_four = (TextView) convertView.findViewById(R.id.tv_number_four);
            holder.tv_number_five = (TextView) convertView.findViewById(R.id.tv_number_five);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        switch (position) {
            case 0:
                holder.tv_number_one.setText(str[0]);
                holder.tv_number_two.setText(str[1]);
                holder.tv_number_three.setText(str[2]);
                holder.tv_number_four.setText(str[3]);
                holder.tv_number_five.setText(str[4]);


                if (mMissData.length >= 5) {
                    holder.tv_miss_data_one.setText(mMissData[0]);
                    holder.tv_miss_data_two.setText(mMissData[1]);
                    holder.tv_miss_data_three.setText(mMissData[2]);
                    holder.tv_miss_data_four.setText(mMissData[3]);
                    holder.tv_miss_data_five.setText(mMissData[4]);
                }
                break;
            case 1:
                holder.tv_number_one.setText(str[5]);
                holder.tv_number_two.setText(str[6]);
                holder.tv_number_three.setText(str[7]);
                holder.tv_number_four.setText(str[8]);
                holder.tv_number_five.setText(str[9]);

                if (mMissData.length >= 10) {
                    holder.tv_miss_data_one.setText(mMissData[5]);
                    holder.tv_miss_data_two.setText(mMissData[6]);
                    holder.tv_miss_data_three.setText(mMissData[7]);
                    holder.tv_miss_data_four.setText(mMissData[8]);
                    holder.tv_miss_data_five.setText(mMissData[9]);
                }
                break;
            case 2:
                holder.tv_number_one.setText(str[10]);
                holder.tv_number_two.setText(str[11]);
                holder.tv_number_three.setText(str[12]);
                holder.tv_number_four.setText(str[13]);
                holder.tv_number_five.setText(str[14]);


                if (mMissData.length >= 15) {
                    holder.tv_miss_data_one.setText(mMissData[10]);
                    holder.tv_miss_data_two.setText(mMissData[11]);
                    holder.tv_miss_data_three.setText(mMissData[12]);
                    holder.tv_miss_data_four.setText(mMissData[13]);
                    holder.tv_miss_data_five.setText(mMissData[14]);
                }
                break;
        }

        if (isShowMissData()) {
            holder.tv_miss_data_one.setVisibility(View.VISIBLE);
            holder.tv_miss_data_two.setVisibility(View.VISIBLE);
            holder.tv_miss_data_three.setVisibility(View.VISIBLE);
            holder.tv_miss_data_four.setVisibility(View.VISIBLE);
            holder.tv_miss_data_five.setVisibility(View.VISIBLE);
        } else {
            holder.tv_miss_data_one.setVisibility(View.INVISIBLE);
            holder.tv_miss_data_two.setVisibility(View.INVISIBLE);
            holder.tv_miss_data_three.setVisibility(View.INVISIBLE);
            holder.tv_miss_data_four.setVisibility(View.INVISIBLE);
            holder.tv_miss_data_five.setVisibility(View.INVISIBLE);
        }

        if (isClear) {
            clearSelect(holder);
        }

        //随机
        if (randomNumber != 0 && position < str.length / 5) {
            holder.rl_one.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_two.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_three.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_four.setBackgroundResource(R.drawable.shape_box_ks_up);
            if (randomNumber == Integer.parseInt(holder.tv_number_one.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_one, holder.rl_one);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_two.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_two, holder.rl_two);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_three.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_three, holder.rl_three);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_four.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_four, holder.rl_four);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_five.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_five, holder.rl_five);
            }


            if (position == str.length / 5 - 1) {
                randomNumber = 0;
                isClear = false;
            }
        }
        return convertView;
    }

    private void clearSelect(ViewHolder holder) {
        if (holder.position == 0) {
            selectMap.clear();
            betParamete.clear();
        }
        if (holder.position < str.length / 5) {
            holder.rl_one.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_two.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_three.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_four.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_five.setBackgroundResource(R.drawable.shape_box_ks_up);

            holder.tv_number_one.setTextColor(holder.tv_number_one.getResources().getColor(R.color.white));
            holder.tv_number_two.setTextColor(holder.tv_number_one.getResources().getColor(R.color.white));
            holder.tv_number_three.setTextColor(holder.tv_number_one.getResources().getColor(R.color.white));
            holder.tv_number_four.setTextColor(holder.tv_number_one.getResources().getColor(R.color.white));
            holder.tv_number_five.setTextColor(holder.tv_number_one.getResources().getColor(R.color.white));
        } else {
            isClear = false;
            i.adapterItemSelect(selectMap);
        }
    }
    public void clearAll(){
        for (int i = 0; i < mListView.getChildCount(); i++) {
            View view = mListView.getChildAt(i);
            ViewHolder holder = (ViewHolder) view.getTag();
            clearSelect(holder);
        }
        isClear = false;
        i.adapterItemSelect(selectMap);
    }

    //设置选中
    private void selectMode(TextView tv) {
        tv.setBackgroundResource(R.color.yello);
        tv.setTextColor(Color.parseColor("#139051"));
    }

    //清除选中
    private void clearSelect(TextView tv) {
        tv.setBackgroundResource(R.color.green_1);
        tv.setTextColor(Color.parseColor("#FFFFFF"));
    }


    //快速选号
    private void speedinessSelected(ViewHolder holder, TextView tv, RelativeLayout rl) {
        key = 0;
        key = Integer.parseInt(tv.getText().toString());
        rl.setBackgroundResource(R.drawable.shape_box_ks_down);
        tv.setTextColor(tv.getResources().getColor(R.color.yello));
        holder.setSelected(key, true);
    }


    int key; //键名

    private void setSelected(ViewHolder holder, TextView tv, RelativeLayout rl) {
        key = 0;
        key = Integer.parseInt(tv.getText().toString());
        if (holder.isSelected(key)) {
            if (rl != null) {
                rl.setBackgroundResource(R.drawable.shape_box_ks_up);
                tv.setTextColor(tv.getResources().getColor(R.color.white));
            } else {
                clearSelect(tv);
            }
            holder.removeSelected(key);
        } else {
            if (rl != null) {
                rl.setBackgroundResource(R.drawable.shape_box_ks_down);
                tv.setTextColor(tv.getResources().getColor(R.color.yello));
            } else {
                selectMode(tv);
            }
            holder.setSelected(key, true);
        }
    }

    @Override
    public void onClick(View v) {
        ViewHolder holder = (ViewHolder) v.getTag();
        switch (v.getId()) {
            case R.id.rl_one:
                setSelected(holder, holder.tv_number_one, holder.rl_one);
                break;
            case R.id.rl_two:
                setSelected(holder, holder.tv_number_two, holder.rl_two);
                break;
            case R.id.rl_three:
                setSelected(holder, holder.tv_number_three, holder.rl_three);
                break;
            case R.id.rl_four:
                setSelected(holder, holder.tv_number_four, holder.rl_four);
                break;
            case R.id.rl_five:
                setSelected(holder, holder.tv_number_five, holder.rl_five);
                break;
        }
    }

    private class ViewHolder {
        int position;

        RelativeLayout rl_one;
        RelativeLayout rl_two;
        RelativeLayout rl_three;
        RelativeLayout rl_four;
        RelativeLayout rl_five;

        TextView tv_miss_data_one;
        TextView tv_miss_data_two;
        TextView tv_miss_data_three;
        TextView tv_miss_data_four;
        TextView tv_miss_data_five;

        TextView tv_number_one;
        TextView tv_number_two;
        TextView tv_number_three;
        TextView tv_number_four;
        TextView tv_number_five;

        public void setSelected(int key, boolean value) {
            selectMap.put(key, value);
            Log.i(TAG, "添加后=" + selectMap.size());
            i.adapterItemSelect(selectMap);
        }

        public void removeSelected(int key) {
            selectMap.remove(key);
            Log.i(TAG, "删除后=" + selectMap.size());
            i.adapterItemSelect(selectMap);
        }

        public boolean isSelected(int key) {
            if (selectMap.get(key) == null) {
                return false;
            } else {
                return selectMap.get(key);
            }
        }
    }


    public void setListView(ListView listView) {
        mListView = listView;
    }

    public boolean isShowMissData() {
        return mShowMissData;

    }

    public void addMissDataInfo(String missData) {

        mMissData = missData.split(",");
        //这里我们要做的一个操作就是显示将数据添加到里面去；

        if (mListView.getChildCount() == 0) {
            notifyDataSetChanged();
        } else {
            if(mMissData.length>=15){
                //防止数组越界
                for (int i = 0; i < mListView.getChildCount(); i++) {
                    ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();
                    //没一个条目有五个子view;
                    viewHolder.tv_miss_data_one.setText(mMissData[i * 5 + 0]);
                    viewHolder.tv_miss_data_two.setText(mMissData[i * 5 + 1]);
                    viewHolder.tv_miss_data_three.setText(mMissData[i * 5 + 2]);
                    viewHolder.tv_miss_data_four.setText(mMissData[i * 5 + 3]);
                    viewHolder.tv_miss_data_five.setText(mMissData[i * 5 + 4]);
                }
            }
        }
    }


    public void showMissData() {
        mShowMissData = true;

        if (mListView.getChildCount() == 0) {
            notifyDataSetChanged();

        } else {
            for (int i = 0; i < mListView.getChildCount() ; i++) {
                ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();

                viewHolder.tv_miss_data_one.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_two.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_three.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_four.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_five.setVisibility(View.VISIBLE);
            }

        }
    }

    public void hideMissData() {
        mShowMissData = false;
        int childCount = mListView.getChildCount();
        if (childCount == 0) {
            notifyDataSetChanged();
        } else {

            for (int i = 0; i < mListView.getChildCount(); i++) {
                ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();

                viewHolder.tv_miss_data_one.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_two.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_three.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_four.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_five.setVisibility(View.INVISIBLE);
            }
        }
    }


}
